package com.zy.explore.file.interfaces.facade.v1;

import com.zy.explore.common.interfaces.vo.ResultVO;
import com.zy.explore.file.application.service.MinioService;
import com.zy.explore.file.infrastructure.constant.MinioRouter;
import com.zy.explore.file.interfaces.feign.MinioFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;

/**
 * 建类说明：Minio文件上传服务
 *
 * @author 中国印
 * @date 2022年01月28日 23:09:10
 */
@Api(tags = "Minio文件上传服务")
@RestController
@RequestMapping(MinioRouter.MINIO)
public class MinioFacade implements MinioFeign {

    @Resource
    private MinioService minioService;

    @Override
    @ApiOperation("上传MultipartFile至Minio返回下载地址")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", dataType = "file", dataTypeClass = File.class, name = "file", value = "文件")
    })
    @PostMapping(value = MinioRouter.UPLOAD, headers = "content-type=multipart/form-data", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResultVO<String> upload(@RequestPart("file") MultipartFile file) throws Exception {
        System.out.println("Minio 上传文件名：" + file.getOriginalFilename());
        return ResultVO.getSuccess(minioService.putObject(file));
    }

    @Override
    @ApiOperation("上传MultipartFile至Minio返回下载地址(流式上传)")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", dataType = "file", dataTypeClass = File.class, name = "file", value = "文件")
    })
    @PostMapping(value = MinioRouter.UPLOAD_BY_BYTES, headers = "content-type=multipart/form-data", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResultVO<String> uploadByBytes(@RequestPart("file") MultipartFile file) throws Exception {
        System.out.println("Minio 上传文件名：" + file.getOriginalFilename());
        return ResultVO.getSuccess(minioService.putObjectByBytes(file.getBytes(), file.getOriginalFilename(), file.getSize(), file.getContentType()));
    }

}
